<script>
import MyHeader from "@/components/MyHeader.vue";
import MyList from "@/components/MyList.vue";
import MyFooter from "@/components/MyFooter.vue";
import pubsub from "pubsub-js"

export default {
  name: "App",
  components: {MyHeader, MyList, MyFooter},
  data() {
    return {
      todoList: JSON.parse(localStorage.getItem('todoList')) || []
    }
  },
  methods: {
    todoObjChangeDone(id) {
      console.log(id,'修改状态');
      this.todoList.forEach((todoObj) => {
        if (todoObj.id === id) {
          todoObj.done = !todoObj.done;
        }
      })
    },
    addTodo(todo){
      this.todoList.push(todo);
    },
    deleteTodo(msgName,data){
      //消息订阅的回调函数形参有两个,第一个时消息名称
      console.log('订阅实现删除,消息名:',msgName,'  id:',data);
      this.todoList = this.todoList.filter(todoObj => todoObj.id !== data)
    },
    deleteAllDone(){
      this.todoList = this.todoList.filter(todoObj => !todoObj.done)
    },
    allDoneOrNot(done){
      this.todoList.forEach((todoObj) => {
        todoObj.done = done
      })
    },
    editeName(id,name){
      this.todoList.forEach((obj)=>{
        if (obj.id === id){
          obj.name=name
        }
      })
    }
  },

  watch:{
    todoList:{
      immediate:false,
      deep:true,
      handler(newVal, oldVal){
        console.log("监视属性执行")
        localStorage.setItem('todoList', JSON.stringify(newVal));
      }
    }
  },
  mounted() {
    //修改状态用全局事件总线来处理
    this.$bus.$on('todoObjChangeDone', this.todoObjChangeDone)
    //this.$bus.$on('deleteTodo', this.deleteTodo)
    //删除使用消息订阅来实现
    this.deletePubsubId =  pubsub.subscribe('deleteTodo', this.deleteTodo);

    this.$bus.$on('editeName', this.editeName);

  },
  beforeDestroy() {
    this.$bus.$off(['todoObjChangeDone','editeName'])
    //取消订阅
    pubsub.unsubscribe(this.deletePubsubId);
  }
}
</script>

<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <MyHeader @addTodo="addTodo"/>
      <MyList :todoList="todoList"/>
      <MyFooter :todoList="todoList" @deleteAllDone="deleteAllDone" @allDoneOrNot="allDoneOrNot"/>
    </div>
  </div>

</template>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn-edite {
  color: black;
  background-color: #dddddd;
  border: 1px solid #bd362f;
}

.btn-edite:hover {
  color: black;
  background-color: #dddddd;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}

/*header*/


/*main*/

/*item*/


/*footer*/


</style>
